package com.class5.springboot.htmlcontroller;

import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.entity.vo.TbXtUserVo;
import com.class5.springboot.frame.repo.dto.PageData;
import com.class5.springboot.frame.repo.dto.ParamMap;
import com.class5.springboot.frame.web.SuperRestController;
import com.class5.springboot.frame.web.result.ResultData;
import com.class5.springboot.service.facade.ILoginService;
import com.class5.springboot.service.facade.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class HUserController extends SuperRestController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ILoginService loginService;

    @RequestMapping("/html/getCurrentUser")
    @ResponseBody
    public ResultData getCurrentUser(HttpServletRequest req) {
//        TbXtUser xtUser = userService.getCurrentUser(username,password,getRealIp(req));
        HttpSession session = req.getSession();
        TbXtUserVo xtUser  = (TbXtUserVo)session.getAttribute("xtUser");
        return successWraper(xtUser);
    }

    @RequestMapping("/html/findUserForPage")
    @ResponseBody
    public ResultData findUserForPage(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        String date_s = req.getParameter("date_s");
        String date_e = req.getParameter("date_e");
        String userName = req.getParameter("userName");
        String realName = req.getParameter("realName");
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        param.put("date_s",date_s);
        param.put("date_e",date_e);
        param.put("userName",userName);
        param.put("realName",realName);
        param.put("page",page);
        param.put("size",size);
        PageData pageData = userService.findUserForPage(ParamMap.get(param));
        return successWraper(pageData);
    }

    @RequestMapping("/html/createUser")
    @ResponseBody
    public ResultData createUser(HttpServletRequest req) {
        JSONObject params = getBodyObject(req);
        JSONObject param = new JSONObject();
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String role = "3";//默认普通用户
        String pass = req.getParameter("pass");
        String sex = req.getParameter("sex");
        param.put("userName",username);
        param.put("passWord",pass);
        param.put("userRole",role);
        param.put("phone",phone);
        param.put("email",email);
        param.put("sex",sex);
        param.put("realName",realname);
        loginService.register(param);
        return successWraper("新增成功");
    }

    @RequestMapping("/html/findUserById")
    @ResponseBody
    public ResultData findUserById(HttpServletRequest req) {
        String id = req.getParameter("id");
        log.info("查询用户信息:{}",id);
        TbXtUserVo tbXtUserVo = userService.findUserById(id);
        return successWraper(tbXtUserVo);
    }

    @RequestMapping("/html/updateUserById")
    @ResponseBody
    public ResultData updateUserById(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String realname = req.getParameter("realname");
        String sex = req.getParameter("sex");
        String password = req.getParameter("pass");
        param.put("id",id);
        param.put("userName",username);
        param.put("phone",phone);
        param.put("email",email);
        param.put("realName",realname);
        param.put("sex",sex);
        param.put("passWord",password);
        log.info("修改用户信息:{}",param.toString());
        userService.updateUserById(param);
        return successWraper("修改成功");
    }

    @RequestMapping("/html/changeUserStatus")
    @ResponseBody
    public ResultData changeUserStatus(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        param.put("id",id);
        param.put("status",status);
        log.info("修改用户信息状态:{}",param.toString());
        userService.changeUserStatus(param);
        return successWraper("修改成功");
    }

    @RequestMapping("/html/deleteUserById")
    @ResponseBody
    public ResultData deleteUserById(HttpServletRequest req) {
        String id = req.getParameter("id");
        log.info("删除用户信息状态:{}",id);
        userService.deleteUserById(id);
        return successWraper("删除成功");
    }

    @RequestMapping("/html/deleteUserByIds")
    @ResponseBody
    public ResultData deleteUserByIds(HttpServletRequest req) {
        String ids = req.getParameter("ids");
        log.info("删除用户信息状态:{}",ids);
        userService.deleteUserByIds(ids);
        return successWraper("删除成功");
    }

}
